package common;

import java.io.File;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

//This class has a method which parses the Page XML. 
public class ReadPageXML {

	public HashMap<String, String> parseXML(String xmlFileName)
	{
	 	try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(xmlFileName));

            // normalize text representation
            doc.getDocumentElement ().normalize ();
            
            //Find the root name from the filename
            String temp [] = null;
            temp = xmlFileName.split("\\.xml");
            
            String temp2 [] = null;
            String pathname = temp[0];
            System.out.println("Pathname = " + pathname);
            temp2 = pathname.split("xml");
            
            int i = temp2.length;
            
            String rootName = temp2[i-1].substring(1);
            System.out.println("RootName =" + rootName);
         
            //Actual Parsing code
            NodeList nList = doc.getElementsByTagName(rootName);
            System.out.println("Node list length " + nList.getLength());    
    		
            Node nParent = nList.item(0);
            
            NodeList nChildrensList = nParent.getChildNodes();
            System.out.println(nChildrensList.getLength());
           
            Node child = nParent.getFirstChild();
            System.out.println("Parent node name: " + nParent.getNodeName());
            
            int tCnt = 0;
           
            HashMap<String,String> map = new HashMap<String,String>();
           
            String TagName = null;
            String TagValue = null;
            for (int j = 0; j < nChildrensList.getLength(); j++) {
            	
               if (child.getNodeType() == Node.ELEMENT_NODE ) {	
                 System.out.println("Tag => " + child.getNodeName());
            	   
                 TagName = new String(child.getNodeName()); 
                 if (child.hasChildNodes()) {
                   NodeList nl1 = child.getChildNodes();
                   System.out.println("Value => " + nl1.item(0).getNodeValue().trim());

                   TagValue = new String(nl1.item(0).getNodeValue().trim());
             	    map.put(TagName, TagValue);
            	   }    
                 tCnt++;
               }
               child = child.getNextSibling();
            }  
            return map;
        }catch (SAXParseException err) {
    
        System.out.println ("** Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());
        return null;

        }catch (SAXException e) {
          Exception x = e.getException ();
          ((x == null) ? e : x).printStackTrace ();
          return null;
        }catch (Throwable t) {
          t.printStackTrace ();
          return null;
        }
     }
}
